<template>
  <div>
    <div>
      <!-- 搜索栏 -->
      <el-input style="width: 200px;margin-right: 20px" placeholder="请输入内容" v-model="content"
                prefix-icon="el-icon-search"></el-input>
      <el-input style="width: 200px;margin-right: 20px" placeholder="请输入接收者ID" v-model="receiverId"
                prefix-icon="el-icon-user"></el-input>
      <el-button style="margin-left: 10px;" type="primary" @click="load" class="el-icon-search">搜索</el-button>
      <el-button style="margin-left: 10px;" type="warning" @click="reset" class="el-icon-refresh">重置</el-button>
    </div>

    <div style="margin-top:20px;margin-bottom: 20px;">
      <!-- 新增。批量删除 -->
      <el-button style="margin-right: 10px;" type="success" class="el-icon-plus" @click="save">新增</el-button>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="确定删除这些数据吗？"
          @confirm="deleteBatch"
          @cancel="cancel">
        <el-button slot="reference" type="danger" style="margin-left:5px;" class="el-icon-delete">批量删除</el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe :header-cell-style="getRowClass"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="notificationId" label="ID"></el-table-column>
      <el-table-column prop="sendTime" label="发送时间"></el-table-column>
      <el-table-column prop="content" label="消息内容">
        <template slot-scope="scope">
          <el-card>{{ scope.row.content }}</el-card>
        </template>
      </el-table-column>
      <el-table-column prop="receiverId" label="接收者ID"></el-table-column>
      <el-table-column prop="isRead" label="是否已读">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isRead ? 'success' : 'info'">{{ scope.row.isRead ? '已读' : '未读' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" class="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除这些数据吗？"
              @confirm="handleDelete(scope.row.notificationId)"
              @cancel="cancel">
            <el-button slot="reference" type="danger" style="margin-left:5px;" class="el-icon-delete">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="padding:10px 0;align-content: center;margin-left: 30%;margin-top:30px;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="消息通知信息" :visible.sync="dialogFormVisible" width="30%" @close="handleCancel">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="消息内容" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.content" autocomplete="off" type="textarea" :rows="10"></el-input>
        </el-form-item>
        <el-form-item label="接收者ID" :label-width="formLabelWidth" prop="receiverId">
          <el-input v-model="form.receiverId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否已读" :label-width="formLabelWidth" prop="isRead">
          <el-switch v-model="form.isRead" active-text="已读" inactive-text="未读"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Notifications",
  data() {
    return {
      pageSize: 10,
      pageNum: 1,
      content: '',
      receiverId: '',
      tableData: [],
      dialogFormVisible: false,
      form: {
        content: '',
        receiverId: '',
        isRead: false
      },
      formLabelWidth: '80px',
      multipleSelection: [],
      total: 0,
      rules: {
        content: [
          {required: true, message: '请输入消息内容', trigger: 'blur'}
        ],
        receiverId: [
          {required: true, message: '请输入接收者ID', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.load();
  },
  methods: {
    async load() {
      const response = await this.request.get("/notifications/findPage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          content: this.content,
          receiverId: this.receiverId,
        }
      });
      this.tableData = response.data.records;
      this.total = response.data.total;
    },
    getRowClass({rowIndex, columnIndex}) {
      if (rowIndex === 0) {
        return 'background:#ccc'
      }
    },
    reset() {
      this.content = '';
      this.receiverId = '';
      this.load();
    },
    save() {
      this.dialogFormVisible = true;
      this.form = {
        content: '',
        receiverId: '',
        isRead: false
      };
    },
    handleCancel() {
      this.dialogFormVisible = false;
      this.form = {
        content: '',
        receiverId: '',
        isRead: false
      };
    },
    async handleAdd() {
      const valid = await this.$refs.form.validate();
      if (!valid) {
        return;
      }
      const response = await this.request.post("/notifications/save", this.form);
      if (response.code === '200') {
        if (this.form.notificationId) {
          this.$message.success('编辑成功');
        } else {
          this.$message.success('新增成功');
        }
        this.dialogFormVisible = false;
        this.load();
      } else {
        this.$message.error(response.msg);
      }

    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    async handleDelete(id) {
      if (id) {
        const response = await this.request.delete('/notifications/deleteById/' + id);
        if (response.code === '200') {
          this.$message.success('删除数据成功');
          this.handleCalPageNum();
        } else {
          this.$message.error(response.msg);
        }
      } else {
        this.$message.error('没有id信息，无法删除');
      }
    },
    cancel() {
      this.$message.success('取消操作成功');
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async deleteBatch() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请先选择要删除的数据");
        return;
      }
      const ids = this.multipleSelection.map(v => v.notificationId);
      const response = await this.request.post('/notifications/deleteBatch', ids);
      if (response.code === '200') {
        this.$message.success('批量删除成功');
        this.handleCalPageNum();
      } else {
        this.$message.error(response.msg);
      }

    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.load();
    },
    async handleCalPageNum() {
      const response = await this.request.get("/notifications/findPage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          content: this.content,
          receiverId: this.receiverId,
        }
      });
      this.total = response.data.total;
      this.pageNum = (this.total % this.pageSize === 0) ? (this.total / this.pageSize) : Math.floor((this.total / this.pageSize) + 1);
      if (this.pageNum < 1) {
        this.pageNum = 1;
      }
      this.load();

    }
  }
}
</script>


<style scoped>

</style>
